/**
 *  CC 4.0 BY-SA
 * link：https://blog.csdn.net/chubu3218/article/details/123920304
 *
 * modified by kevin
 */

#include "JsonDataCom.h"

#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QXmlStreamReader>
#include <QtDebug>
#include <QtMath>

#include <fstream>
#include <map>

//---------------------------------
JsonDataCom::JsonDataCom()
    : m_bgDataVec() {

    readData();
    dump();
}
//---------------------------------
void JsonDataCom::dump() {
    for (auto& v : m_bgDataVec) {
        qDebug() << v.toJsonObject();
    }
}
//---------------------------------
QVariantList JsonDataCom::getData() const {
    return m_bgDataVec;
}
//---------------------------------
int JsonDataCom::readData() {
    m_bgDataVec.clear();
    QString url = "JsonDataView.json";
    qDebug() << " JsonDataCom::readData() :" << url;

    QFile file(url);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        qDebug() << "can`t open json : " << url;
        return 1;
    }

    QTextStream stream(&file);
    stream.setCodec("UTF-8");
    QString         str = stream.readAll();
    QJsonParseError jsonErr;
    QJsonDocument   doc = QJsonDocument::fromJson(str.toUtf8(), &jsonErr);
    if (jsonErr.error != QJsonParseError::NoError && !doc.isNull()) {
        qDebug() << "json error!!" << jsonErr.error;
        return 2;
    }

    QJsonObject rootObj  = doc.object();
    QJsonValue  rootName = rootObj.value("bgData");
    if (rootName.type() == QJsonValue::Array) {
        QJsonArray bgArr = rootName.toArray();
        for (int i = 0; i < bgArr.count(); i++) {
            QJsonValue childValue = bgArr.at(i);
            //
            if (childValue.type() == QJsonValue::Object) {
                //
                QVariantMap bgTempData;
                QJsonObject childObj = childValue.toObject();
                //
                bgTempData[ "disUp" ]   = childObj.value("disUp").toDouble();
                bgTempData[ "disDown" ] = childObj.value("disDown").toDouble();
                bgTempData[ "spdUp" ]   = childObj.value("spdUp").toDouble();
                bgTempData[ "spdDown" ] = childObj.value("spdDown").toDouble();
                m_bgDataVec.append(bgTempData);
            }
        }
    }
    file.close();
    return 0;
}
//---------------------------------
int JsonDataCom::saveData(QVariantList bgData) {
    m_bgDataVec.clear();
    QString url = "JsonDataView.json";
    qDebug() << " JsonDataCom::saveData() :" << url;
    QJsonArray bgArr;
    for (auto& v : bgData) {
        try {
            auto        m = v.toMap();
            QJsonObject rootTempObj;
            rootTempObj.insert("disUp", m[ "disUp" ].toDouble());
            rootTempObj.insert("disDown", m[ "disDown" ].toDouble());
            rootTempObj.insert("spdUp", m[ "spdUp" ].toDouble());
            rootTempObj.insert("spdDown", m[ "spdDown" ].toDouble());
            bgArr.append(rootTempObj);
            // v.toJsonObject()
        }
        catch (...) {
            qDebug() << "setBackDataFromView error!";
            return 3;
        }
    }

    QJsonObject rootObj;
    rootObj.insert("bgData", bgArr);
    QJsonDocument doc;
    doc.setObject(rootObj);
    QFile file(url);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        qDebug() << "have no JsonDataView.json!";
        return 4;
    }
    QTextStream stream(&file);
    stream.setCodec("UTF-8");
    stream << doc.toJson();
    file.close();
    return 0;
}
